/*
*  compareTwoPic.java
*  This class compares two bitmap pictures and returns similarity coefficient
*  
*  Public function - static double differencesTwoPic(Bitmap object, Bitmap image)
*/

package com.eltech.battleofrunes;

import android.graphics.Bitmap;

public class compareTwoPic
{
	private static double maxDist = 15; // This is max Hamming distance, that have profit in game
	private static double coef = Math.PI / (maxDist * 2);// Coefficient for build distribution
	
	// This function return coefficient by differences between two pictures
	public static double differencesTwoPic(Bitmap object, Bitmap image)
	{
		double differences = 0; // Result of comparison of two bitmap
		
		// Object
		// Array to bitmap
		int [] objectArray;
		objectArray = new int[object.getWidth() * object.getHeight()];
		
		// Copy pixel data from the Bitmap into the intArray
		object.getPixels(objectArray, 0, object.getWidth(), 0, 0, object.getWidth(), object.getHeight());
		
		// Image
		// Array to bitmap
		int [] imageArray = new int[image.getWidth() * image.getHeight()];
		
		// Copy pixel data from the Bitmap into the intArray
		image.getPixels(imageArray, 0, image.getWidth(), 0, 0, image.getWidth(), image.getHeight());
		
		// Calculate Hamming distance
		int dist = calcDist(object.getHeight(), object.getWidth(), objectArray, image.getHeight(), image.getWidth(), imageArray);
		
		// Calculate the coefficient of distribution
		if (dist >= maxDist)
			differences = 0;
		else
			differences = Math.cos(dist*coef);
		
		return differences;
	}
	
	// Native c++ function
	private static native int calcDist(int object_w, int object_h, int [] objectArray, int image_w, int image_h, int [] imageArray);
	
	// Load my native c++ library
	static 
    {
        System.loadLibrary("imageHash");
    }
}